home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Disc to the Future 2
/
Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin
/
MAC
/
MACSHELL
/
MS1
/
COMMANDS
/
BUILTIN.C
< prev
next >
Wrap
Text File
|
1992-12-02
|
17KB
|
712 lines
/*
* MacShell Source File
*
* Copyright (c) 1989, 1990, 1991, 1992 Suick Bay Technologies. All rights reserved.
*
*
* RESTRICTIONS ON MacShell program and source code.
*
* Ñ╩MacShell¬ is a product of Suick Bay Technologies and is provided for
* restricted use by the owner of the CDROM "Disk to the future II".
*
* Ñ╩No permission is granted for any commercial use without the written
* consent of the Suick Bay Technologies.
*
* Ñ╩No permission is granted for any redistribution of any kind use without
* the written consent of the Suick Bay Technologies.
*
* Ñ╩Permission is granted to use this for any personal noncommercial use.
*
* Ñ╩You may not distribute source or executable code at all, nor may you
* distribute it with or within a commercial product without the written
* consent of the Suick Bay Technologies. Please send modifications to
* the author for inclusion in updates to the program. Thanks.
*
*
* MacShell¬ IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
* WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
* PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
*
* SUICK BAY TECHNOLOGIES SHALL HAVE NO LIABILITY WITH RESPECT TO THE
* INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY MACSHELL
* OR ANY PART THEREOF.
*
* In no event will Suick Bay Technologies be liable for any lost revenue
* or profits or other special, indirect and consequential damages, even if
* Suick Bay Technologies has been advised of the possibility of such damages.
*
* Suick Bay Technologies can be reached at:
*
* 8768 Cottonwood lane
* Maple Grove, MN 55369
* Voice: (612) 425-7025
* AppleLink: D5233
*
*
* No parts of this software may be reproduced or stored in a
* retrieval system or transmitted in any form, or any means,
* electronic, mechanical, photocopying, recording or otherwise,
* without the prior written permission of Suick Bay Technologies.
*
* Spread the word and not the disk.
*
* SPK 020790 : Moved alias/unalias commands to new file Alias.c
* Updated ResStrs to use types
* Added arguments to alias'd commands
* SPK 012290 : Initial
*/
#include "SystemPub.h"
#include "Proc.h"
#include "Path.h"
#include "ShellPub.h"
extern int16 appResFile;
#define USERMENUBASE 200
/*******************************************************************/
void AddShellResStr( char *ref, char *string, int32 type )
{
char buf[ 256 ], **cp;
int16 id;
strcpy( buf, ref );
CtoPstr( buf );
CtoPstr( string );
cp = (char **) NewString( string );
UseResFile( appResFile );
id = Unique1ID( type );
AddResource( cp, type, id, buf );
PtoCstr( string );
}
void AddShellString( char *ref, char *string )
{
AddShellResStr( ref, string, STRRES );
}
/*******************************************************************/
char **GetShellResStr( char *ref, int32 type )
{
char buf[ 256 ], **ch;
strcpy( buf, ref );
CtoPstr( buf );
UseResFile( appResFile );
ch = (char **) GetNamedResource( type, buf );
return( ch ); /* handle to string */
}
char **GetShellString( char *ref )
{
return( GetShellResStr( ref, STRRES ) );
}
/*******************************************************************/
void RemShellResStr( char *ref, int32 type )
{
char buf[ 256 ], **cp;
strcpy( buf, ref );
CtoPstr( buf );
cp = (char **) GetNamedResource( type, buf );
if( cp )
{
RmveResource( cp );
DisposHandle( cp );
}
}
void RemShellString( char *ref )
{
RemShellResStr( ref, STRRES );
}
/*******************************************************************/
void RemShellResStrs( int32 type )
{
int16 i;
Handle h;
UseResFile( appResFile );
i = Count1Resources( type );
SetResLoad( FALSE );
while( i )
{
h = GetIndResource( type, i );
RmveResource( h );
DisposHandle( h );
i--;
}
SetResLoad( TRUE );
}
void RemShellStrings()
{
RemShellResStrs( STRRES );
RemShellResStrs( ALSRES );
}
/*******************************************************************/
DoUserMenu( int16 item, MHandle Mh )
{
char *cp, *np, menuName[ 256 ], itemName[ 256 ], **ch;
int16 i;
if( currShell == NULL ) /* can't operate without a shell */
return;
GetItem( (**Mh).mhMenu, item, itemName );
/*
* Get the command string
* Execute the command sring
*/
cp = (char *) (**(**Mh).mhMenu).menuData;
i = *cp + 1;
np = menuName;
while( i-- )
*np++ = *cp++;
PtoCstr( menuName );
PtoCstr( itemName );
strcat( menuName, itemName );
ch = GetShellString( menuName );
if( ch )
{
CopyStr( *ch, itemName );
PtoCstr( itemName );
strcat( itemName, "\n" );
CommandString( itemName, currShell );
}
}
/*******************************************************************/
StripMetaChars( char *str )
{
char *s, *l;
s = str;
l = str;
while( *l )
{
switch( *l )
{
case '^' : /* icon number */
case '!' : /* mark character */
case '<' : /* B, I, U, O, S style */
case '/' : /* command key */
*l++;
case '(' : /* disable item */
case ';' : /* separate item */
*l++;
break;
default :
*s++ = *l++;
}
}
*s = *l; /* add the NULL */
}
/*******************************************************************/
int16 menuID = USERMENUBASE;
Boolean BuiltInCMD( WHandle ShellWh, int16 ProcID, int16 ShellProcID,
char *command )
{
WHandle tempWh = 0L;
ShellWindRec **MyShell;
char argument[ 256 ];
int16 i, argc;
MyShell = (ShellWindRec **) (**ShellWh).thing;
argc = (**MyShell).Proc[ ProcID ].argc;
tempWh++;
if( strcmp( "new", command ) == 0 )
ShellWindInit( "\pShell" );
else if( strcmp( "addmenu", command ) == 0 && (argc > 1))
{
MHandle Mh;
MenuHandle theMenu;
char item[ 256 ];
char name[ 256 ];
int16 found, menuItems;
GetArgv( ShellWh, ProcID, 1, argument ); /* menu name */
if( argc > 2 )
GetArgv( ShellWh, ProcID, 2, item ); /* item name */
/* Does the menu exist ? */
Mh = GetMhByName( argument );
if( Mh == NULL ) /* no, make menu */
{
CtoPstr( argument );
theMenu = NewMenu( menuID++, argument );
CtoPstr( item );
InsMenuItem( theMenu, item, 31 );
SBTMenu( theMenu, &DoUserMenu, NULL );
/* Save the command string */
if( argc > 3 )
{
PtoCstr( argument );
PtoCstr( item );
StripMetaChars( item );
strcat( argument, item );
GetArgv( ShellWh, ProcID, 3, item ); /* command string */
AddShellString( argument, item );
}
}
else if( argc > 2 ) /* yes, now the item */
{
if( (**Mh).mhID < USERMENUBASE )
return( TRUE );
/* Does the item exist ? */
found = FALSE;
menuItems = CountMItems( (**Mh).mhMenu );
StripMetaChars( item );
for( i = 1; i <= menuItems; i++ )
{
GetItem( (**Mh).mhMenu, i, name );
PtoCstr( name );
if( strcmp( name, item ) == 0 )
{
/* found it */
found = TRUE;
break;
}
}
if( found )
{
/* redo the item because it could have different meta-chars */
/* redo the command */
if( argc > 3 )
{
/* Remove the old command string */
strcat( argument, item );
RemShellString( argument );
/* Save the new command string */
GetArgv( ShellWh, ProcID, 3, item ); /* command string */
AddShellString( argument, item );
}
}
else
{
GetArgv( ShellWh, ProcID, 2, item ); /* item name */
CtoPstr( item );
InsMenuItem( (**Mh).mhMenu, item, 0 );
/* Save the command string */
if( argc > 3 )
{
PtoCstr( item );
StripMetaChars( item );
strcat( argument, item );
GetArgv( ShellWh, ProcID, 3, item ); /* command string */
AddShellString( argument, item );
}
}
}
}
else if( (strcmp( "addsubmenu", command ) == 0) && (argc > 3) )
{
char str1[ 64 ], str2[ 64 ], name[ 64 ];
MHandle theMh, inMh;
MenuHandle theMenu;
int16 found, menuItems, inMenuItem;
GetArgv( ShellWh, ProcID, 1, str1 ); /* menu name */
GetArgv( ShellWh, ProcID, 2, str2 ); /* item name */
/* Does the menu exist ? */
inMh = GetMhByName( str1 );
if( inMh )
{
if( (**inMh).mhID < USERMENUBASE )
return( TRUE );
/* Does the item exist ? */
found = FALSE;
menuItems = CountMItems( (**inMh).mhMenu );
StripMetaChars( str2 );
for( i = 1; i <= menuItems; i++ )
{
GetItem( (**inMh).mhMenu, i, name );
PtoCstr( name );
if( strcmp( str2, name ) == 0 )
{
/* found it */
found = TRUE;
break;
}
}
if( found ) /* found the menu */
{
inMenuItem = i;
GetArgv( ShellWh, ProcID, 3, str1 ); /* sub menu name */
theMh = GetMhByName( str1 );
if( theMh && (**theMh).mhID < USERMENUBASE )
return( TRUE );
if( theMh && argc > 4 ) /* sub menu exsists */
{
found = FALSE;
menuItems = CountMItems( (**theMh).mhMenu );
GetArgv( ShellWh, ProcID, 4, str2 ); /* sub menu name */
StripMetaChars( str2 );
for( i = 1; i <= menuItems; i++ )
{
GetItem( (**theMh).mhMenu, i, name );
PtoCstr( name );
if( strcmp( str2, name ) == 0 )
{
/* found it */
found = TRUE;
break;
}
}
if( found ) /* sub menu item exists */
{
if( argc > 5) /* command exists */
{
/* Remove the old command string */
strcat( str1, str2 );
RemShellString( str1 );
/* Save the new command string */
GetArgv( ShellWh, ProcID, 5, str2 ); /* command string */
AddShellString( str1, str2 );
}
}
else /* new item */
{
GetArgv( ShellWh, ProcID, 4, str2 ); /* item name */
CtoPstr( str2 );
InsMenuItem( (**theMh).mhMenu, str2, 0 );
/* Save the command string */
if( argc > 5 )
{
PtoCstr( str2 );
StripMetaChars( str2 );
strcat( str1, str2 );
GetArgv( ShellWh, ProcID, 5, str2 ); /* command string */
AddShellString( str1, str2 );
}
}
}
else /* make sub menu */
{
GetArgv( ShellWh, ProcID, 3, str1 );
CtoPstr( str1 );
theMenu = NewMenu( menuID++, str1 );
GetArgv( ShellWh, ProcID, 4, str2 );
CtoPstr( str2 );
InsMenuItem( theMenu, str2, 31 );
SBTHierMenu( theMenu, (**inMh).mhMenu, inMenuItem, &DoUserMenu, NULL);
/* Save the command string */
if( argc > 5 )
{
PtoCstr( str1 );
PtoCstr( str2 );
StripMetaChars( str2 );
strcat( str1, str2 );
GetArgv( ShellWh, ProcID, 5, str2 ); /* command string */
AddShellString( str1, str2 );
}
}
}
}
}
else if( strcmp( "delmenu", command ) == 0 && (argc > 1))
{
MHandle Mh;
char item[ 64 ];
char name[ 64 ];
int16 found, menuItems;
GetArgv( ShellWh, ProcID, 1, argument ); /* menu name */
if( argc > 2 )
GetArgv( ShellWh, ProcID, 2, item ); /* item name */
/*
* Does the menu exist ?
*/
Mh = GetMhByName( argument );
if( Mh != NULL ) /* delete menu */
{
if( (**Mh).mhID < USERMENUBASE )
return( TRUE );
if( argc > 2 )
{
found = TRUE;
while( found && !UserAbort()) /* remove all occurences */
{
found = FALSE;
menuItems = CountMItems( (**Mh).mhMenu );
StripMetaChars( item );
for( i = 1; i <= menuItems; i++ )
{
GetItem( (**Mh).mhMenu, i, name );
PtoCstr( name );
if( StrPatMatch( name, item ) )
{
/* found it */
found = TRUE;
break;
}
}
GetArgv( ShellWh, ProcID, 2, item ); /* item name */
if( found )
{
DelMenuItem( (**Mh).mhMenu, i );
/* Remove the command string */
strcat( argument, item );
RemShellString( argument );
}
}
}
else /* remove the menu */
{
char str[ 256 ];
/* Remove the command strings */
menuItems = CountMItems( (**Mh).mhMenu );
for( i = 1; i < menuItems; i++ )
{
GetItem( (**Mh).mhMenu, i, name );
PtoCstr( name );
strcpy( str, argument );
strcat( argument, item );
RemShellString( argument );
}
SBTRmveMenu( (**Mh).mhMenu, TRUE );
DrawMenuBar();
}
}
}
else if( strcmp( "open", command ) == 0 )
{
extern Boolean read;
if( argc == 1 )
WindowMessage( ShellWh, OpenMsg );
else
{
read = FALSE;
for( i = 1; i < argc; i++ )
{
GetArgv( ShellWh, ProcID, i, argument );
LoadFileIntoWind( ShellWh, ProcID, argument );
}
}
}
else if( strcmp( "close", command ) == 0 )
{
for( i = 1; i < argc; i++ )
{
GetArgv( ShellWh, ProcID, i, argument );
while( tempWh && !UserAbort() )
{
/* don't scan 'From' here because of changing list */
tempWh = GetWhByName( argument );
if( tempWh != ShellWh )
WindowMessage( tempWh, CloseMsg );
}
}
}
else if( strcmp( "save", command ) == 0 )
{
#ifndef DEMO
for( i = 1; i < argc; i++ )
{
GetArgv( ShellWh, ProcID, i, argument );
while( tempWh && !UserAbort() )
{
tempWh = GetWhByNameFrom( argument, tempWh );
WindowMessage( tempWh, SaveMsg );
}
}
#else
printf( "Demo version, save is not available\n" );
#endif
}
else if( strcmp( "saveas", command ) == 0 )
{
#ifndef DEMO
for( i = 1; i < argc; i++ )
{
GetArgv( ShellWh, ProcID, i, argument );
while( tempWh && !UserAbort() )
{
tempWh = GetWhByNameFrom( argument, tempWh );
WindowMessage( tempWh, SaveAsMsg );
}
}
#else
printf( "Demo version, saveas is not available\n" );
#endif
}
else if( strcmp( "revert", command ) == 0 )
{
for( i = 1; i < argc; i++ )
{
GetArgv( ShellWh, ProcID, i, argument );
while( tempWh && !UserAbort() )
{
tempWh = GetWhByNameFrom( argument, tempWh );
if( tempWh != ShellWh )
WindowMessage( tempWh, RevertMsg );
}
}
}
else if( strcmp( "pagesetup", command ) == 0 )
{
DoPageSetup();
}
else if( strcmp( "print", command ) == 0 )
{
if( argc == 1 )
{
tempWh = GetWHandler(FrontWindow());
if( tempWh )
WindowMessage( tempWh, PrintMsg );
}
else
for( i = 1; i < argc; i++ )
{
GetArgv( ShellWh, ProcID, i, argument );
while( tempWh && !UserAbort() )
{
tempWh = GetWhByNameFrom( argument, tempWh );
WindowMessage( tempWh, PrintMsg );
}
}
}
else if( strcmp( "quit", command ) == 0 )
SBTQuit();
else if( strcmp( "undo", command ) == 0 ) {}
else if( strcmp( "select", command ) == 0 ) {}
else if( strcmp( "cut", command ) == 0 ) {}
else if( strcmp( "copy", command ) == 0 ) {}
else if( strcmp( "paste", command ) == 0 ) {}
else if( strcmp( "clear", command ) == 0 )
{
if( argc == 1 ) /* clear the shell */
ClearShellOutput( ShellWh );
}
else if( strcmp( "shutdown", command ) == 0 )
ShutDwnPower();
else if( strcmp( "restart", command ) == 0 )
ShutDwnStart();
else if( strcmp( "sh", command ) == 0 )
ShellWindInit( "\pShell" );
else if( strcmp( "about", command ) == 0 )
DoAbout();
else if( strcmp( "clean", command ) == 0 )
CleanUpWindows();
else if( strcmp( "sdb", command ) == 0 )
{
printf( "MyShell %lX\n", MyShell );
printf( " file name .. %ps\n",(**MyShell).fileName );
printf( " new ........ %d\n", (**MyShell).new );
printf( " dirty ...... %d\n", (**MyShell).dirty );
printf( " vRefNum .... %d\n", (**MyShell).vRefNum );
printf( " wantsInput . %d\n", (**MyShell).wantsInput );
printf( " readOnly ... %d\n", (**MyShell).readOnly );
printf( " numProcs ... %d\n", (**MyShell).numProcs );
printf( " prompt ..... %d\n", (**MyShell).prompt );
printf( " promptBase . %ld\n",(**MyShell).promptBase );
printf( " pwdVRefNum . %d\n", (**MyShell).pwdVRefNum );
printf( " pwdDirID ... %ld\n", (**MyShell).pwdDirID );
printf( " parDirID ... %ld\n", (**MyShell).parDirID );
printf( " shellID .... %d\n", (**MyShell).shellID );
{
int16 vRefNum;
int32 dirID, parDirID;
GetPWDInfo( &vRefNum, &dirID, &parDirID );
printf( "PWD From OS : vRef %d dirID %ld parDirID\n", vRefNum, dirID, parDirID );
}
}
else if( (strcmp( "help", command ) == 0) || (strcmp( "?", command ) == 0) )
procPrintf( ShellWh, ShellProcID, "try man or usage.\n" );
else /* could it be an alias ? */
{
char alias[ 256 ];
char **ch = GetShellResStr( command, ALSRES );
if( ch )
{
CopyStr( *ch, alias );
PtoCstr( alias );
/* add the arguments to the aliased command */
for( i = 1; i < argc; i++ )
{
GetArgv( ShellWh, ProcID, i, argument );
strcat( alias, " " );
strcat( alias, argument );
}
strcat( alias, "\n" );
CommandString( alias, ShellWh );
}
else
return( FALSE );
}
return( TRUE );
}